<?php
namespace app\member\controller;
use think\Controller;
use PHPMailer\PHPMailer\PHPMailer;
use ChuanglanSmsHelper\ChuanglanSmsApi;
use PHPMailer\PHPMailer\Exception;
// use app\member\controller\Base;
require 'vendor/phpmailer/phpmailer/src/Exception.php';
require 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
require 'vendor/phpmailer/phpmailer/src/SMTP.php';
class Account  
{
    public function reg(){
		$data = input('post.');
		if(request()->isPost()){
			// $validate = validate('User');
			// if(!$validate->check($data)){
			// 	$this->error($validate->getError());
			// }
			$userData = array();
			$userData['username']=trim($data['username']);
			$userData['password']=md5($data['password']);
			$userData['email'] = $data['email'];
			$userData['mobile_phone'] = $data['mobile_phone'];
			$userData['register_type']=$data['register_type'];
			$userData['register_time']=time();
			$add=db('user')->insert($userData);
			if($add){
				$loginRes = $this->login(1);
				if($loginRes['error'] == 0){
					$this->success('注册成功!正在为你跳转...','member/User/index');
				}else{
					$this->success('注册成功!正在为您跳转...','member/Account/login');
				}
				
			}else{
				$this->error('注册失败！');
			}
			// dump($userData);die;
		}
		return view();
	}
	//type = 0 说明需要为客户端返回json对象,type = 1说明要为服务端返回普通数组类型
	public function login($type=0){
		 if(request()->isPost()){
			$data = input('post.');
			if(isset($data['back_act'])){
				$backAct=$data['back_act'];
			}else{
				$backAct='';
			}
				return model('User')->login($data,$type,$backAct);
		 }
		 return view();
	}
	// public function checkLogin(){
	// 	$uid=session('uid');
	// 	if($uid){
	// 		$arr['error']=0;
	// 		$arr['uid']=$uid;
	// 		$arr['username']=session('username');
	// 		return json($arr);
	// 	}else{
	// 		if(cookie('username') && cookie('password')){
	// 			$data['username']=encryption(cookie('username'),1);
	// 			$data['password']=encryption(cookie('password'),1);
	// 			$loginRes = model('user')->login($data,1);
	// 			if($loginRes['error'] == 0){
	// 				$arr['error']=0;
	// 				$arr['uid']=0;
	// 				$arr['username']=session('username');
	// 				return json($arr);
	// 			}
	// 		}
	// 		$arr = array();
	// 		$arr['error']=1;
	// 		return json($arr);
	// 	}
	// }
	//判断用户是否已经登录
	public function checkLogin(){
		$uid = session('uid');
		if($uid){
			$arr['error']=0;
			$arr['uid']=$uid;
			$arr['username']=session('username');
			return json($arr);
		}else{
			if(cookie('username') && cookie('password')){
				$data['username'] = encryption(cookie('username'),1);
				$data['password'] = encryption(cookie('password'),1);
				$loginRes = model('user')->login($data,1);
				if($loginRes['error'] == 0){
					$arr['error']=0;
					$arr['uid']=$uid;
					$arr['username']=session('username');
					return json($arr);
				}
			}
			$arr=array();
			$arr['error']=1;
			return json($arr);
		}
	}
	//type: 0 代表注册场景 1:代表手机找回密码场景 2向用户发送密码
	public function sendMsg($type = 0,$password = 0){
		$clapi  = new ChuanglanSmsApi();
		$code = mt_rand(100000,999999);
		$tipMsg = '';
		if($password == 0){
			$tipMsg = '您的验证码是'.$code;
		}else{
			
			$tipMsg = '您的新密码是'.$password.'请妥善保管';
		}
		if($password == 0){
			$phoneNum = trim(input('phoneNum'));
		}else{
			
			$phoneNum = session('getPasswordPhoneNum');
		}
		
		$result = $clapi->sendSMS($phoneNum,  $tipMsg);
		if(!is_null(json_decode($result))){  //
		    $output=json_decode($result,true);
		    if(isset($output['code'])  && $output['code']=='0'){
				if($type == 0){
					session('mobileCode',$code);
				}else{
					session('getPasswordCode',$code);
					session('getPasswordPhoneNum',$phoneNum);
				}
		        $msg = ['status'=>0,'msg'=>'发送成功'];
				return json($msg);
		    }else{
		    $msg = ['status'=>1,'msg'=>$output['errorMsg']];
			return json($msg);
			}
		}else{
			$msg = ['status'=>2,'msg'=>'内部错误'];
		       return json($msg);
		}
	}
	public function sendMail($email = '', $password = ''){
			if($email){
				$to = $email;
			}else{
				$to = input('email');
			}
			$code = mt_rand(100000,999999);
			
			$content = '';
			
			if($password){
				$content = '您的新密码是:'.$password.'请妥善保管';
			}else{
				$content = '您的验证码是:'.$code;
			}
			
			//邮件调试模式
			$mail = new PHPMailer();
		            
		            //设置邮件使用SMTP
		            $mail->isSMTP();
		            // 设置邮件程序以使用SMTP
		            $mail->Host = 'smtp.qq.com';
		            // 设置邮件内容的编码
		            $mail->CharSet='UTF-8';
		            // 启用SMTP验证
		            $mail->SMTPAuth = true;
		            // SMTP username
		            $mail->Username = '3580389349';
		            // SMTP password
		            $mail->Password = 'dufbzmdwjavncjcb';
		            // 启用TLS加密，`ssl`也被接受
		//            $mail->SMTPSecure = 'tls';
		            // 连接的TCP端口
		//            $mail->Port = 587;
		            //设置发件人
		            $mail->setFrom('3580389349@qq.com', '发件人');
		           //  添加收件人1
		            $mail->addAddress($to, '111');     // Add a recipient
		//            $mail->addAddress('ellen@example.com');               // Name is optional
		//            收件人回复的邮箱
		            $mail->addReplyTo('3580389349@qq.com', 'fajian');
		//            抄送
		//            $mail->addCC('cc@example.com');
		//            $mail->addBCC('bcc@example.com');
		            //附件
		//            $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
		//            $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
		            //Content
		            // 将电子邮件格式设置为HTML
		            $mail->isHTML(true);
		            $mail->Subject = '**点灯';
		            $mail->Body    = $content;
		//            $mail->AltBody = '这是非HTML邮件客户端的纯文本';
		           $sendRes =  $mail->send();
		           
		            $mail->isSMTP();
					
		if($sendRes){
			//记录邮件验证码放入session中
			session('emailCode',$code);
			
		    $msg = ['status'=>0,'msg'=>'发送成功','password' => $password];
			return json($msg);
		}else{
		$msg = ['status'=>1,'msg'=>'发送失败'];
		return json($msg);
		}
	}
	//验证用户名是否注册或者已经注册
	public function isRegistered(){
		if(request()->isAjax()){
			$username =input('username');
			
			$userInfo = db('User')->where(array('username'=>$username))->find();
			if($userInfo){
				return false;
			}else{
				return  true;
			}
		}else{
			$this->error('非法请求');
		}
	}
	//验证手机号是否注册
	public function checkPhone(){
		if(request()->isAjax()){
			$mobilephone =input('mobile_phone');
			$userInfo = db('user')->where(array('mobile_phone'=>$mobilephone))->find();
			if($userInfo){
				return false;
			}else{
				return  true;
			}
		}else{
			$this->error('非法请求');
		}
	}
	//验证邮箱是否可以注册
	public function checkEmail(){
			if(request()->isAjax()){
				$email =input('email');
				$userInfo = db('user')->where(array('email'=>$email))->find();
				if($userInfo){
					return false;
				}else{
					return  true;
				}
		}else {
			$this->error('非法请求!');
		}
	}
	//异步验证邮箱验证码
	public function checkEmailSendCode(){
		if(request()->isAjax()){
				$emailCode =input('send_code');
				if($emailCode == session('emailCode')){
					return true;
				}else{
					return  false;
				}
		}else {
			$this->error('非法请求!');
		}
	}
	//异步验证手机验证码
	public function codeNotice(){
		if(request()->isAjax()){
			$emailCode =input('mobile_code');
			if($emailCode == session('mobileCode')){
				return true;
			}else{
				return  false;
			}
		}else{
			$this->error('非法请求!');
		}
	}
	//找回密码
	public function getPassword(){
		return view();
	}
	//验证手机号并发送短信
	public function checkSendMsg(){
			$data=input('post.');
			$phoneNum=trim($data['phoneNum']);
			if($phoneNum){
				$users = db('user')->where(array('mobile_phone'=>$phoneNum))->find();
				if($users){
					return  $this->sendMsg(1);
				}else{
					$arr=array();
					$arr['msg'] = '用户不存在！';
					$arr['status'] = 1;
					return json($arr);
				}
			}else{
				$arr=array();
				$arr['msg'] = '请填写手机号！';
				$arr['status'] =1;
				return json($arr);
			}
	}
	//找回密码时候验证手机验证码是否正确  点击提交的时候请求到该方法 checkPhoneCode
	public function checkPhoneCode(){
		$data = input('post.');
		$mobileCode = trim($data['mobile_code']);
		$sCode = session('getPasswordCode');
		$mobilePhone=session('getPasswordPhoneNum');
		if($sCode == $mobileCode){
			$password = mt_rand(100000,999999);
			$_password = md5($password);
			$update = db('user')->where(array('mobile_phone'=>$mobilePhone))->update(['password'=>$_password]);
			if($update){
				$this->sendMsg(2,$password);
			}else{
				return false;
			}
		}else{
			return false;
		}
	}
	//通过用户名和邮箱找回密码
	public function sendPwdEmail(){
		
		// dump(input('post.'));die;
		$data = input('post.');
		$userData['username'] = trim($data['user_name']);
		$userData['email'] = trim($data['email']);
		//信息对比
		$users = db('user')->where(array('username'=>$userData['username']))->find();
		if($users){
			if($users['email'] == $userData['email']){
				$password = mt_rand(100000,999999);
				$_password = md5($password);
				
				$update = db('user')->where(array('username'=>$userData['username']))->update(['password'=>$_password]);
				if($update){
					$_msg = $this->sendMail($userData['email'],$password);
					$msg['status'] = 0;
					$msg['msg'] = '修改密码成功';
					
				}else{
					$msg['status'] = 3;
					$msg['msg'] = '修改密码失败';
				}
				
				
			}else{
				$msg['status']=2;
				$msg['msg'] = '您填写的电子邮箱地址错误，请重新输入';
			}
		}else{
			$msg['status'] = 1;
			$msg['msg'] = '您填写的用户名不存在,请重新输入!';
		
		}
		$this->assign([
			'show_right'=>1,
			'status'=>$msg['status'],
			'msg'=>$msg['msg'],
		]);
		return view('index@common/tip_info');
	}
}
